﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SQuestioninfo.cs" company="hoandd   ">
// hoandd
// </copyright>
// <summary>
//   Summary description for SQuestioninfo
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace DesktopModules.Surveys
{
    using System;
    using System.Data;
    using DotNetNuke.Common.Utilities;
    using Microsoft.ApplicationBlocks.Data;

    /// <summary>
    /// Summary description for SqlDataProvider
    /// </summary>
    public class SqlDataProvider : DataProvider
    {
        private const string ProviderType = "data";
        private DotNetNuke.Framework.Providers.ProviderConfiguration _providerConfiguration = DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;

        public SqlDataProvider()
        {
            DotNetNuke.Framework.Providers.Provider objProvider = ((DotNetNuke.Framework.Providers.Provider)(_providerConfiguration.Providers[_providerConfiguration.DefaultProvider]));

            //  This code handles getting the connection string from either the connectionString / appsetting section and uses the connectionstring section by default if it exists.
            //  Get Connection string from web.config
            _connectionString = Config.GetConnectionString();
            //  If above funtion does not return anything then connectionString must be set in the dataprovider section.
            if ((_connectionString == ""))
            {
                //  Use connection string specified in provider
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];
            _objectQualifier = objProvider.Attributes["objectQualifier"];
            if (_objectQualifier != "" & _objectQualifier.EndsWith("_") == false)
            {
                _objectQualifier += "_";
            }
            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if (_databaseOwner != "" & _databaseOwner.EndsWith(".") == false)
            {
                _databaseOwner += ".";
            }
        }

        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
        }

        public string ProviderPath
        {
            get
            {
                return _providerPath;
            }
        }

        public string ObjectQualifier
        {
            get
            {
                return _objectQualifier;
            }
        }

        public string DatabaseOwner
        {
            get
            {
                return _databaseOwner;
            }
        }

        public override int AddSQuestion(int OrgID, byte QuestionType, string QuestionContent, bool Hidden, bool Public, DateTime DateCreated, DateTime LastModified, int CreatedByUserID, int ModifiedByUserID, int ModuleId)
        {
            return (Convert.ToInt32((SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + "EL_AddSQuestion", OrgID, QuestionType, QuestionContent, Hidden, Public, DateCreated, LastModified, CreatedByUserID, ModifiedByUserID, ModuleId))));
        }

        public override IDataReader GetSQuestion(int SQuestionID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "EL_GetSQuestion", SQuestionID);
        }

        public override IDataReader GetSQuestions()
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "EL_GetSQuestions");
        }

        public override void UpdateSQuestion(int SQuestionID, int OrgID, byte QuestionType, string QuestionContent, bool Hidden, bool Public, DateTime DateCreated, DateTime LastModified, int CreatedByUserID, int ModifiedByUserID, int ModuleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_UpdateSQuestion", SQuestionID, OrgID, QuestionType, QuestionContent, Hidden, Public, DateCreated, LastModified, CreatedByUserID, ModifiedByUserID, ModuleId);
        }

        public override void DeleteSQuestion(int sQuestionID)
        {
            throw new NotImplementedException();
        }

        public override void AddSQuestionAnswer(int SQuestionID, string Answer, int ViewOrder)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_AddSQuestionAnswer", SQuestionID, Answer, ViewOrder);
        }

        public override IDataReader GetSQuestionAnswer(int SAnswerID)
        {
            throw new NotImplementedException();
        }

        public override IDataReader GetSQuestionAnswers()
        {
            throw new NotImplementedException();
        }

        public override IDataReader GetSQuestionAnswers(int sQuestionID)
        {
            return ((IDataReader)(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "GetSurveyOptions", sQuestionID)));
        }

        public override void UpdateSQuestionAnswer(int SAnswerID, int SQuestionID, string Answer, int ViewOrder)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_UpdateSQuestionAnswer", SAnswerID, SQuestionID, Answer, ViewOrder);
        }

        public override void DeleteSQuestionAnswer(int SAnswerID)
        {
            throw new NotImplementedException();
        }

        #region Public Methods Surveys of table Surveys

        /// -----------------------------------------------------------------------------
        ///<summary>
        /// The SQLDataProvider Surveys  for table  Surveys
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------

        public override int AddSurveys(int OrgID, string Title, bool Public, System.DateTime DateCreated, System.DateTime LastModified, int CreatedByUserID, int ModifiedByUserID, string Description)
        {
            return (Convert.ToInt32((SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + "EL_AddSurveys", OrgID, Title, Public, DateCreated, LastModified, CreatedByUserID, ModifiedByUserID, Description))));
        }

        public override void DeleteSurveys(int SurveyID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_DeleteSurveys", SurveyID);
        }

        public override IDataReader GetSurveys(int SurveyID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "EL_GetSurveys", SurveyID);
        }

        public override IDataReader GetSurveyss()
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "EL_GetSurveyss");
        }

        public override void UpdateSurveys(int SurveyID, int OrgID, string Title, bool Public, System.DateTime DateCreated, System.DateTime LastModified, int CreatedByUserID, int ModifiedByUserID, string Description)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_UpdateSurveys", SurveyID, OrgID, Title, Public, DateCreated, LastModified, CreatedByUserID, ModifiedByUserID, Description);
        }

        #endregion Public Methods Surveys of table Surveys

        #region Public Methods SurveyQuestion of table SurveyQuestion

        /// -----------------------------------------------------------------------------
        ///<summary>
        /// The SQLDataProvider SurveyQuestion  for table  SurveyQuestion
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------

        public override void AddSurveyQuestion(int SurveyID, int SQuestionID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_AddSurveyQuestion", SurveyID, SQuestionID);
        }

        public override void DeleteSurveyQuestion(int SurveyQuestionID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_DeleteSurveyQuestion", SurveyQuestionID);
        }

        public override IDataReader GetSurveyQuestion(int SurveyQuestionID)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "EL_GetSurveyQuestion", SurveyQuestionID);
        }

        public override IDataReader GetSurveyQuestions()
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + "EL_GetSurveyQuestions");
        }

        public override void UpdateSurveyQuestion(int SurveyQuestionID, int SurveyID, int SQuestionID)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + "EL_UpdateSurveyQuestion", SurveyQuestionID, SurveyID, SQuestionID);
        }

        #endregion Public Methods SurveyQuestion of table SurveyQuestion
    }
}